﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;
using DWEIP.Models.tsm;
using DWEIP.Models.Sys;
using DataWin.Core.Model;

namespace DWEIP.BLL
{
    public class HomeBLL
    {
        public List<SysModuleModel> GetTopMenuByPersonId(string personId)
        {
            string xmlPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"Config\ModuleResource.xml");
            Byte[] _Bytes = null;
            using (MemoryStream _MemoryStream = new MemoryStream())
            {
                using (Stream _Stream = File.OpenRead(xmlPath))
                {
                    _Bytes = new byte[_Stream.Length];
                    _Stream.Read(_Bytes, 0, _Bytes.Length);
                }
            }

            XmlDocument doc = new XmlDocument();
            List<SysModuleModel> mList = new List<SysModuleModel>();

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(_Bytes))
            {
                doc.Load(ms);

                XmlNodeList ToolbarNodeList = doc.SelectNodes("/WebEip/ModulesManager/Modulebar");
                int sortModule = 0;
                foreach (XmlNode ToolbarNode in ToolbarNodeList)
                {
                    if (ToolbarNode.Attributes["Key"].Value == "MainModuleBar")
                    {
                        //只取頂級
                        foreach (XmlNode _node in ToolbarNode.ChildNodes)
                        {
                            if (_node.Name == "PopupMenuTool")
                            {
                                SysModuleModel newModule = new SysModuleModel();
                                newModule.Id = _node.Attributes["Key"].Value; ;
                                newModule.Text = _node.Attributes["Text"].Value;
                                if (_node.Attributes["Iconic"]!=null)
                                {
                                    newModule.Iconic = _node.Attributes["Iconic"].Value;
                                }
                                if (_node.Attributes["Url"] != null)
                                {
                                    newModule.Url = _node.Attributes["Url"].Value;
                                }

                                newModule.Sort = ++sortModule;

                                mList.Add(newModule);
                            }
                        }
                    }
                }

            }
            return mList;
        }

        public List<SysModuleModel> GetTreeByPersonId(string personId, string parentId)
        {
            string xmlPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"Config\ModuleResource.xml");
            Byte[] _Bytes = null;
            using (MemoryStream _MemoryStream = new MemoryStream())
            {
                using (Stream _Stream = File.OpenRead(xmlPath))
                {
                    _Bytes = new byte[_Stream.Length];
                    _Stream.Read(_Bytes, 0, _Bytes.Length);
                }
            }

            XmlDocument doc = new XmlDocument();
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(_Bytes))
            {
                doc.Load(ms);
                List<SysModuleModel> mList = new List<SysModuleModel>();
                XmlNodeList ToolbarNodeList = doc.SelectNodes("/WebEip/ModulesManager/Modulebar");
                foreach (XmlNode ToolbarNode in ToolbarNodeList)
                {
                    if (ToolbarNode.Attributes["Key"].Value == "MainModuleBar")
                    {
                        GetTreeList(mList, ToolbarNode.ChildNodes, parentId, parentId=="");
                        return mList;
                    }
                }

            }
            return null;
        }

        public bool GetTreeList(List<SysModuleModel> ModuleList, XmlNodeList ToolbarNode, string ParentId, bool InParent)
        {
            int sortModule = 0;
            foreach (XmlNode _node in ToolbarNode)
            {
                if (_node.Name == "PopupMenuTool")
                {
                    String Curkey = _node.Attributes["Key"].Value;
                    if (Curkey == ParentId && ParentId.Length > 0)
                    {
                        GetTreeList(ModuleList, _node.ChildNodes, Curkey, true);
                        return true;
                    }
                    if (InParent)
                    {
                        SysModuleModel newModule = new SysModuleModel();
                        newModule.Id = _node.Attributes["Key"].Value; ;
                        newModule.Text = _node.Attributes["Text"].Value;
                        if (_node.Attributes["Iconic"] != null)
                        {
                            newModule.Iconic = _node.Attributes["Iconic"].Value;
                        }
                        if (_node.Attributes["Url"] != null)
                        {
                            newModule.Url = _node.Attributes["Url"].Value;
                        }
                        newModule.ParentId = ParentId;
                        newModule.Sort = ++sortModule;

                        ModuleList.Add(newModule);
                    }
                    else
                    {
                        if (GetTreeList(ModuleList, _node.ChildNodes, ParentId, false))
                        {
                            return true;
                        }
                    }
                }
                else if (_node.Name == "ButtonTool")
                {
                    if (InParent)
                    {
                        SysModuleModel newModule = new SysModuleModel();
                        newModule.Id = _node.Attributes["Key"].Value; ;
                        newModule.Text = _node.Attributes["Text"].Value;
                        if (_node.Attributes["Iconic"] != null)
                        {
                            newModule.Iconic = _node.Attributes["Iconic"].Value;
                        }
                        if (_node.Attributes["Url"] != null)
                        {
                            newModule.Url = _node.Attributes["Url"].Value;
                        }
                        newModule.ParentId = ParentId;
                        newModule.Sort = ++sortModule;

                        ModuleList.Add(newModule);
                    }
                }
            }
            return false;
        }
    }
}
